/*
 * Copyright 2010 Mttang.com All right reserved. This software is the confidential and proprietary information of
 * Mttang.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with Mttang.com.
 */
package com.mttang.sys.biz.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mttang.framework.core.action.AbstractAction;
import com.mttang.framework.core.exception.MtTangServiceRuntimeException;
import com.mttang.framework.core.paginated.util.PagedFliper;
import com.mttang.framework.core.pojos.ServiceRequest;
import com.mttang.framework.core.pojos.ServiceResponse;
import com.mttang.info.core.ParameterModel;
import com.mttang.info.core.ResultDTO;
import com.mttang.info.core.ResultDTOWrapper;
import com.mttang.sys.biz.service.DepartmentService;
import com.mttang.sys.biz.service.RoleService;
import com.mttang.sys.biz.service.UserService;
import com.mttang.sys.dto.TreeDTO;
import com.mttang.sys.dto.UserDTO;
import com.mttang.util.InfoAppKeyConstant;
import com.mttang.util.SysBeanConstant;

/**
 * 类UserAction.java的实现描述：TODO 类实现描述
 * 
 * @author acc 2010-6-1 上午10:32:11
 */
public class UserAction extends AbstractAction {

	private UserService userService;

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	private static final String STATUS_FALSE = "false";

	private static final String STATUS_TRUE = "true";

	private Log logger = LogFactory.getLog(getClass());


	@SuppressWarnings("unchecked")
	public Map<Object, Object> query(Map<String, Object> map2) {
		ParameterModel parameterModel = new ParameterModel(); // 解析查询参数
		PagedFliper pagedFliper = (PagedFliper) map2
				.get(InfoAppKeyConstant.K_pageModel);
		Map<String, Object> pMap = (Map<String, Object>) map2
				.get(InfoAppKeyConstant.K_parameter);
		parameterModel.setPagedFliper(pagedFliper);
		parameterModel.setPramater(pMap);
		ResultDTO<UserDTO> list = userService.findUser(parameterModel);
		ResultDTOWrapper wrapper = new ResultDTOWrapper(list);
		wrapper.setPagedFliper(pagedFliper); // 重新设置分页信息
		Map<Object, Object> map = wrapper.wrap();
		RoleService rs = (RoleService) context
				.getBean(SysBeanConstant.SERVICE_ROLE);
		List<Map<String, String>> rolelist = rs.findAll();
		map.put("rolelist", rolelist);
		return map;
	}

	public Map<Object, Object> insert(Map<String, Object> map2) {
		// 添加
		UserDTO userDto = userService.findByEmpno(String.valueOf(map2
				.get("empno")));
		Map<Object, Object> map = new HashMap<Object, Object>();
		if (userDto != null) {
			map.put("userid", userDto.getId());
			return map;
		}
		UserDTO dto = new UserDTO();
		if (map2.get("empno") != null
				&& !"".equals(String.valueOf(map2.get("empno")))
				&& !"null".equals(map2.get("empno"))) {
			dto.setEmpno(String.valueOf(map2.get("empno")));
		}
		if (map2.get("passWord") != null
				&& !"".equals(String.valueOf(map2.get("passWord")))
				&& !"null".equals(map2.get("passWord"))) {
			dto.setPassWord(String.valueOf(map2.get("passWord")));
		}
		if (map2.get("name") != null
				&& !"".equals(String.valueOf(map2.get("name")))
				&& !"null".equals(map2.get("name"))) {
			dto.setName(String.valueOf(map2.get("name")));
		}
		if (map2.get("depId") != null
				&& !"".equals(String.valueOf(map2.get("depId")))
				&& !"null".equals(map2.get("depId"))) {
			dto.setDepId((Integer) map2.get("depId"));
		}
		if (map2.get("email") != null
				&& !"".equals(String.valueOf(map2.get("email")))
				&& !"null".equals(map2.get("email"))) {
			dto.setEmail(String.valueOf(map2.get("email")));
		} else {
			dto.setEmail("");
		}
		if (map2.get("phone") != null
				&& !"".equals(String.valueOf(map2.get("phone")))
				&& !"null".equals(map2.get("phone"))) {
			dto.setPhone(String.valueOf(map2.get("phone")));
		} else {
			dto.setPhone("");
		}
		if (map2.get("roleId") != null
				&& !"".equals(String.valueOf(map2.get("roleId")))
				&& !"null".equals(map2.get("roleId"))) {
			dto.setRoleId(String.valueOf(map2.get("roleId")));
			dto.setRoleName(String.valueOf(map2.get("roleName")));
		}
		dto.setState(1);
		Map<Object, Object> logMap = (Map<Object, Object>) map2.get("LOGMAP");
		userService.insert(dto, logMap);
		String subject = (String) map2.get("subject");
		String content = (String) map2.get("content");
		String errorkey = null;
		String strStatus = STATUS_FALSE;
		int result = -1;
		switch (result) {
		case -1:
			strStatus = STATUS_FALSE;
			errorkey = "OK";
			break;
		case -2:
			strStatus = STATUS_FALSE;
			errorkey = "error";
			break;
		case 0:
			strStatus = STATUS_TRUE;
			break;
		default:
			strStatus = STATUS_FALSE;
		}
		map.put("status", strStatus);
		return map;

	}

	public Map<Object, Object> list(Map<Object, Object> map2) {

		// 查询部门和角色的LIST
		Map<Object, Object> map = new HashMap<Object, Object>();
		RoleService rs = (RoleService) context
				.getBean(SysBeanConstant.SERVICE_ROLE);
		DepartmentService depservice = (DepartmentService) context
				.getBean(SysBeanConstant.SERVICE_DEPARTMENT);
		List<Map<String, String>> list = rs.findAll();
		List<TreeDTO> deplist = depservice.qryDept(map2);
		map.put("rolelist", list);
		map.put("deplist", deplist);
		return map;

	}

	public Map<Object, Object> update(Map<String, Object> map2) {
		// 修改
		if (!String.valueOf(map2.get("empno")).equals(
				String.valueOf(map2.get("empno1")))) {
			UserDTO userDto = userService.findByEmpno(String.valueOf(map2
					.get("empno")));
			Map<Object, Object> map = new HashMap<Object, Object>();
			if (userDto != null) {
				map.put("userid", userDto.getId());
				return map;
			}
		}
		UserDTO dto = new UserDTO();
		dto.setId((Integer) map2.get("id"));
		if (map2.get("empno") != null
				&& !"".equals(String.valueOf(map2.get("empno")))
				&& !"null".equals(map2.get("empno"))) {
			dto.setEmpno(String.valueOf(map2.get("empno")));
		}
		if (map2.get("passWord") != null
				&& !"".equals(String.valueOf(map2.get("passWord")))
				&& !"null".equals(map2.get("passWord"))) {
			dto.setPassWord(String.valueOf(map2.get("passWord")));
		}
		if (map2.get("name") != null
				&& !"".equals(String.valueOf(map2.get("name")))
				&& !"null".equals(map2.get("name"))) {
			dto.setName(String.valueOf(map2.get("name")));
		}
		if (map2.get("depId") != null
				&& !"".equals(String.valueOf(map2.get("depId")))
				&& !"null".equals(map2.get("depId"))) {
			dto.setDepId((Integer) map2.get("depId"));
		}
		if (map2.get("email") != null
				&& !"".equals(String.valueOf(map2.get("email")))
				&& !"null".equals(map2.get("email"))) {
			dto.setEmail(String.valueOf(map2.get("email")));
		} else {
			dto.setEmail("");
		}
		if (map2.get("phone") != null
				&& !"".equals(String.valueOf(map2.get("phone")))
				&& !"null".equals(map2.get("phone"))) {
			dto.setPhone(String.valueOf(map2.get("phone")));
		} else {
			dto.setPhone("");
		}
		if (map2.get("roleId") != null
				&& !"".equals(String.valueOf(map2.get("roleId")))
				&& !"null".equals(map2.get("roleId"))) {
			dto.setRoleId(String.valueOf(map2.get("roleId")));
			dto.setRoleName(String.valueOf(map2.get("roleName")));
		}
		if (map2.get("state") != null
				&& !"".equals(String.valueOf(map2.get("state")))
				&& !"null".equals(map2.get("state"))) {
			dto.setState((Integer) map2.get("state"));
		}
		Map<Object, Object> logMap = (Map<Object, Object>) map2.get("LOGMAP");
		userService.update(dto, logMap);
		return null;
	}
	
	public Map<Object,Object> serarchById(Map<String,Object> map2){

		// 根据ID查询
		UserDTO userDto = userService.findById((Integer) map2
				.get("id"));
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("userid", userDto.getId());
		map.put("name", userDto.getName());
		map.put("passWord", userDto.getPassWord());
		map.put("phone", userDto.getPhone());
		map.put("email", userDto.getEmail());
		map.put("createtime", userDto.getCreateTime());
		map.put("state", userDto.getState());
		map.put("depId", userDto.getDepId());
		map.put("roleId", userDto.getRoleId());
		map.put("empno", userDto.getEmpno());
		map.put("roleName", userDto.getRoleName());
		map.put("depName", userDto.getDepName());
		return map;
	
	}
	
  @SuppressWarnings("unchecked")
public Map<String,Object> deleteUpdate(Map<String,Object> map2){

		// 修改为删除状态
		UserDTO dto = new UserDTO();
		dto.setId((Integer) map2.get("id"));
		dto.setState(-1);
		Map<Object, Object> logMap = (Map<Object, Object>) map2.get("LOGMAP");
		userService.delete(dto, logMap);
		return null;
	
  }
  
  @SuppressWarnings("unchecked")
public void pass(Map<String,Object> map2){
		// 密码重置
		UserDTO dto = new UserDTO();
		dto.setId((Integer) map2.get("id"));
		dto.setPassWord(String.valueOf(map2.get("passWord")));
		Map<Object, Object> logMap = (Map<Object, Object>) map2.get("LOGMAP");
		userService.update(dto, logMap);
	}
  
  
  public Map<Object,Object> empno(Map<String,Object> map2){
		UserDTO userDto = userService.findByEmpno(String.valueOf(map2.get("empno")));
		Map<Object, Object> map = new HashMap<Object, Object>();
		if (userDto != null) {
			map.put("userid", userDto.getId());
		}
		return map;
	
  }
  
  public Map<Object,Object> fingUserList(Map<String,Object> map2){
	  // 查询用户列表不分页
		List<Map<String, Object>> list = userService
				.findAllUserList(map2);
		Map<Object, Object> model = new HashMap<Object, Object>();
		model.put("result", list);
		return model;
  }

@Override
public ServiceResponse perform(ServiceRequest arg0)
		throws MtTangServiceRuntimeException {
	// TODO Auto-generated method stub
	return null;
}
  
  
}
